package cn.com.thingo.hyintelligent.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.model.bean.ControlInfraredBean;
import cn.com.thingo.hyintelligent.model.bean.ControlSmartBean;

public class InfraredListAdapter extends RecyclerView.Adapter<InfraredListAdapter.ViewHolder> {
    private List<ControlInfraredBean> list;
    private Context context;
    private OnItemClickListener listener;

    public InfraredListAdapter(List<ControlInfraredBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_smart_list, parent, false));

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        ControlInfraredBean controlInfraredBean = list.get(position);
        holder.ivIcon.setImageResource(controlInfraredBean.getIcon());
        holder.tvName.setText(controlInfraredBean.getName());
        if (listener!= null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onInfraredItemClick(list.get(position).getType());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_smart_icon)
        ImageView ivIcon;
        @BindView(R.id.item_smart_name)
        TextView tvName;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onInfraredItemClick(int type);
    }
}
